package com.tqjc.system.core.init;

import com.tqjc.system.common.constant.AddTaskConstant;
import com.tqjc.system.common.constant.ConfigConstant;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.enums.TankCarNoEnum;
import com.tqjc.system.core.gate.service.SysGateService;
import com.tqjc.system.core.pipeline.thread.SendTankPositionThread;
import com.tqjc.system.core.threadpool.ThreadPoolManager;
import com.tqjc.system.system.entity.SysConfigEntity;
import com.tqjc.system.system.service.SysConfigService;
import com.tqjc.system.system.util.SystemUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.tqjc.system.system.service.SystemService;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.*;

/**
 * 初始化启动执行
 * @author teli_
 *
 */
@Slf4j
@Configuration
public class CommonStartConfig implements InitializingBean, DisposableBean {

	@Resource(name = "systemService")
	private SystemService systemService;

	@Autowired
	private SysGateService sysGateService;

	@Autowired
	private SysConfigService configService;
	
	/**
	 * spring初始化执行
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("---------------初始化启动执行---------------");
		
		//系统初始化数据字典
		systemService.initDictionary();
		//系统初始化部门数据
		systemService.initDeparts();

		//槽车线程启动
		initTankCarThread();

		//socket通讯服务启动
//		systemService.initCommunicateTask();

		//道闸初始化
		//sysGateService.init();


	}

	private void initTankCarThread(){
		String tankCarNo1 = TankCarNoEnum.NO_1.getTankCarNo();
		String tankCarNo2 = TankCarNoEnum.NO_2.getTankCarNo();
		String tankCarNo3 = TankCarNoEnum.NO_3.getTankCarNo();
		//队列初始化
		AddTaskConstant.TANK_CAR_QUEUE_MAP.put(tankCarNo1,new LinkedList());
		AddTaskConstant.TANK_CAR_QUEUE_MAP.put(tankCarNo2,new LinkedList());
		AddTaskConstant.TANK_CAR_QUEUE_MAP.put(tankCarNo3,new LinkedList());
		//线程初始化
		SendTankPositionThread thread1 = SendTankPositionThread.builder().tankCarNo(tankCarNo1).build();
		AddTaskConstant.TANK_CAR_THREAD_MAP.put(tankCarNo1,thread1);
		SendTankPositionThread thread2 = SendTankPositionThread.builder().tankCarNo(tankCarNo2).build();
		AddTaskConstant.TANK_CAR_THREAD_MAP.put(tankCarNo2,thread2);
		SendTankPositionThread thread3 = SendTankPositionThread.builder().tankCarNo(tankCarNo3).build();
		AddTaskConstant.TANK_CAR_THREAD_MAP.put(tankCarNo3,thread3);
		//启动线程
		ThreadPoolManager.getInstance().execute(thread1);
		ThreadPoolManager.getInstance().execute(thread2);
		ThreadPoolManager.getInstance().execute(thread3);

		//初始化天车sn
		Map<String,String> carBakSnMap = new HashMap<>(4);
		carBakSnMap.put("1号废钢跨天车","BakS_f378923e769c4e6f8256204dc793bb9c");
		carBakSnMap.put("2号废钢跨天车","BakS_e8960391b16f47dea8408b060e15b3d0");
		carBakSnMap.put("1号渣钢跨天车","BakS_8df30c5ec48c4001989cf913ca9b0c0c");
		carBakSnMap.put("2号渣钢跨天车","BakS_2aed777dc6c446a2afc25dea9e4767df");
		carBakSnMap.put("过跨槽车","BakS_e7fdbf40925a49a897761645eaba4977");
		SystemUtils.putCarInfo(ConfigConstant.CONFIG_TYPE_CAR_BAK_SN,carBakSnMap);

		//是否启动槽车扫描
		SysConfigEntity tankCarConfigInfo = configService.getConfigInfo(ConfigConstant.CONFIG_TYPE_SWITCH, ConfigConstant.CONFIG_ID_TANK_CAR_SCAN);

		if(tankCarConfigInfo != null){
			AddTaskConstant.TANKCAR_SCAN_USE_FLAG = Boolean.valueOf(tankCarConfigInfo.getConfigValue());
		}


		//是否检查天车模式开关
		SysConfigEntity carStatusConfig = configService.getConfigInfo(ConfigConstant.CONFIG_TYPE_SWITCH, ConfigConstant.CONFIG_ID_CAR_STATUS);

		if(carStatusConfig != null){
			AddTaskConstant.CAR_STATUS_FLAG = Boolean.valueOf(carStatusConfig.getConfigValue());
		}


		//是否开启参观模式开关
		SysConfigEntity visitFlagConfig = configService.getConfigInfo(ConfigConstant.CONFIG_TYPE_SWITCH, ConfigConstant.CONFIG_ID_VISIT_FLAG);

		if(visitFlagConfig != null){

			GlobalConstant.VISIT_MODEL_FLAG = Boolean.valueOf(visitFlagConfig.getConfigValue());
		}

	}

	
	/**
	 * 容器销毁执行
	 */
	@Override
	public void destroy() throws Exception {
		log.info("---------------容器销毁启动执行---------------");
		
		//关闭线程池
		ThreadPoolManager.getInstance().shutdown();

		//断开道闸摄像头连接
		sysGateService.disconnect();

		
		log.info("---------------容器销毁启动执行完成---------------");
	}
	

}
